package com.atguigu.order.controller;

import com.atguigu.utils.JwtUtils;
import com.atguigu.utils.R;
import com.atguigu.order.entity.TOrder;
import com.atguigu.order.service.TOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-07-16
 */
@RestController
@Api(value = "TOrderController", description = "8007订单接口")
@CrossOrigin
@RequestMapping("/order/tOrder")
public class TOrderController {

    @Resource
    private TOrderService tOrderService;

    @ApiOperation("生成订单信息 返还订单id 不是订单号!")
    @PostMapping("/createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest request) {
        String tokenId = JwtUtils.getToken(request);
        if (StringUtils.isEmpty(courseId)) {
            return R.error();
        }
        String tOrderId = tOrderService.createOrder(courseId, tokenId);
        return R.ok().data("tOrderId", tOrderId);
    }

    @ApiOperation("根据订单id 查询订单信息!")
    @PostMapping("/getOrder/{id}")
    public R getOrderById(@PathVariable String id) {
        TOrder tOrder = tOrderService.getById(id);
        if (tOrder == null) {
            return R.error().message("null");
        }
        return R.ok().data("tOrder", tOrder);
    }

}

